home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / imageio.lib / dev / examples / memorysave / main.c next >
Encoding:
C/C++ Source or Header  |  2000-08-09  |  4.5 KB  |  183 lines

  1. /* This example use of imageio.library loads the image file specified
  2.         on the command line at half size and saves it to the file 'ram:test'.
  3. */
  4.  
  5. #include <stdio.h>
  6.  
  7. #include <dos/dos.h>
  8. #include <exec/memory.h>
  9. #include <exec/types.h>
  10.  
  11. #include <clib/dos_protos.h>
  12. #include <clib/exec_protos.h>
  13. #include <clib/cybergraphics_protos.h>
  14. #include <clib/intuition_protos.h>
  15.  
  16. #include <pragmas/dos_pragmas.h>
  17. #include <pragmas/exec_pragmas.h>
  18. #include <pragmas/intuition_pragmas.h>
  19. #include <pragmas/cybergraphics_pragmas.h>
  20.  
  21. #include <cybergraphics/cybergraphics.h>
  22.  
  23. #include <imageio/imageio.h>
  24. #include <imageio/imageio_protos.h>
  25. #include <imageio/imageio_pragmas.h>
  26.  
  27. #include <guigfx/guigfx.h>
  28. #include <pragmas/guigfx_pragmas.h>
  29. #include <guigfx/guigfx_protos.h>
  30.  
  31. /* Function prototypes */
  32. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  33.  
  34. extern struct Library *DOSBase;
  35. struct Library *ImageIOBase, *IntuitionBase;
  36.  
  37. void main( int argc, char **argv )
  38. {
  39.     if ( argv[1] != NULL )
  40.     {
  41.         ImageIOBase = OpenLibrary( "imageio.library", 2 );
  42.         IntuitionBase = OpenLibrary( "intuition.library", NULL );
  43.         if ( IntuitionBase && ImageIOBase )
  44.         {
  45.             struct ImageHandle *ih;
  46.             ULONG err;
  47.             BPTR fp;
  48.  
  49.             fp = Open( argv[1], MODE_OLDFILE );
  50.             if ( fp != NULL )
  51.             {
  52.                 err = AllocImage( &ih, IMG_SrcFile, fp, TAG_DONE );
  53.                 if ( !err )
  54.                 {
  55.                     ULONG num = 1, denom = 2;
  56.                     ULONG x, y, bpp, rs;
  57.                     UBYTE *buffer, cs, it;
  58.  
  59.                     err = GetImageAttrs( ih,
  60.                         IMG_Width, &x,
  61.                         IMG_Height,&y,
  62.                         IMG_BytesPerPixel, &bpp,
  63.                         IMG_ColourSpace, &cs,
  64.                         IMG_RowSize, &rs,
  65.                         IMG_TestScaleNum, num,
  66.                         IMG_TestScaleDenom, denom,
  67.                         TAG_DONE );
  68.                     if ( !err )
  69.                     {
  70.                         printf( "width=%ld, height=%ld\n", x, y );
  71.                         printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  72.                         printf( "row size=%ld\n", rs );
  73.  
  74.                         err = ReadImage( ih,
  75.                             IMG_ScaleNum, num,
  76.                             IMG_ScaleDenom, denom,
  77.                             IMG_ImageBuffer, &buffer,
  78.                             IMG_ProgressHook, progressFunc,
  79.                             TAG_DONE );
  80.                         if ( !err )
  81.                         {
  82.                             err = GetImageAttrs( ih,
  83.                                 IMG_ImageType, &it,
  84.                                 TAG_DONE );
  85.                             if ( !err )
  86.                             {
  87.                                 printf("Image type=%d\n",it);
  88.                                 printf("Image buffer=%ld\n",buffer);
  89.                             }
  90.  
  91.                             /* Save the image */
  92.                             {
  93.                                 UBYTE saveable;
  94.                                 struct ImageHandle *memih;
  95.                                 UBYTE *newbuffer;
  96.  
  97.                                 err = AllocImage( &memih,
  98.                                     IMG_RGBBuffer, buffer,
  99.                                     IMG_Width, x,
  100.                                     IMG_Height, y,
  101.                                     IMG_ColourSpace, IMCS_RGB,
  102.                                     IMG_BytesPerPixel, 3,
  103.                                     TAG_DONE );
  104.                                 if ( !err )
  105.                                 {
  106.                                     saveable = IMGF_JPEG;
  107.                                     err = GetImageAttrs( memih,
  108.                                         IMG_QuerySaveable, &saveable,
  109.                                         TAG_DONE );
  110.                                     if ( !err )
  111.                                     {
  112.                                         printf( "can save in JPEG: %s\n", saveable == 1 ? "Yes" : "No" );
  113.                                     }
  114.  
  115.                                     if ( saveable == 1 )
  116.                                     {
  117.                                         err = GetImageAttrs( memih,
  118.                                             IMG_Width, &x,
  119.                                             IMG_Height,&y,
  120.                                             IMG_BytesPerPixel, &bpp,
  121.                                             IMG_ColourSpace, &cs,
  122.                                             IMG_RowSize, &rs,
  123.                                             IMG_ImageBuffer, &newbuffer,
  124.                                             TAG_DONE );
  125.                                         if ( !err )
  126.                                         {
  127.                                             printf( "width=%ld, height=%ld\n", x, y );
  128.                                             printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  129.                                             printf( "row size=%ld\n", rs );
  130.                                             printf( "new buffer=%ld\n", newbuffer );
  131.                                         }
  132.  
  133.                                         err = WriteImage( memih, IMGF_JPEG, "ram:test",
  134.                                             IMG_ProgressHook, progressFunc,
  135.                                             IMG_JPEG_Quality, 90,
  136.                                             TAG_DONE );
  137.                                         if ( !err )
  138.                                         {
  139.                                             printf("Image saved\n");
  140.                                         }
  141.                                         else printf( "write image error:%d\n", err );
  142.                                     }
  143.  
  144.                                     FreeImage( memih );
  145.                                 }
  146.                                 else printf( "alloc image failed:%d\n", err );
  147.                             }
  148.                         }
  149.                         else printf( "read image error:%d\n", err );
  150.                     }
  151.                     else printf( "get image attrs error:%d\n", err );
  152.  
  153.                     FreeImage( ih );
  154.                 }
  155.                 else printf( "alloc image error:%d\n", err );
  156.  
  157.                 Close( fp );
  158.             }
  159.             else printf( "cant open file\n" );
  160.         }
  161.  
  162.         if ( ImageIOBase ) CloseLibrary( ImageIOBase );
  163.         if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  164.     }
  165.     else printf( "no file specified\n" );
  166. }
  167.  
  168. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  169. {
  170.     static int prevpercent = 0;
  171.  
  172.     int percent = ( curr * 100 ) / lines;
  173.  
  174.     if ( prevpercent != percent )
  175.     {
  176.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  177.     }
  178.  
  179.     prevpercent = percent;
  180.  
  181.     return NULL;
  182. }
  183.